package com.base.kongjian.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import cn.mysteel.util.ObjectUtils;

import com.base.kongjian.dao.RealInformDAO;
import com.base.kongjian.entity.RealInform;
import com.base.kongjian.service.RealInformService;
import com.mysteel.page.Page;

@Service ("realInformService")
public class RealInformServiceImpl implements RealInformService
{
	@Autowired
	private RealInformDAO realInformDao;

	@Override
	public void saveRI(RealInform realInform)
	{
	
		Assert.notNull (realInform);
		
		RealInform r = realInformDao.getByMemberId (realInform.getMemberId ());
		
		if(ObjectUtils.isNull (r))
		{
			realInformDao.save (realInform);
		}
	}

	@Override
	public void deleteRIById(long id)
	{
		RealInform r = realInformDao.getById (id);
		
		if(ObjectUtils.notNull (r))
		{
			realInformDao.deleteByMemberId (r.getMemberId ());
		}
	}

	@Override
	public void deleteRIByMemberId(long memberId)
	{
		
		realInformDao.deleteByMemberId (memberId);
	}

	@Override
	public void update(RealInform realInform)
	{
		/*private String name;				//姓名
		private long phone;					//电话
		private long sfNum;					//身份证号
		private String address;				//地址
		private String hobby;				//一项兴趣爱好
*/		Assert.notNull (realInform);
		RealInform r = realInformDao.getById (realInform.getId ());
		if(ObjectUtils.notNull (r))
		{
			r.setName (realInform.getName ());
			r.setPhone (realInform.getPhone ());
			r.setSfNum (realInform.getSfNum ());
			r.setAddress (realInform.getAddress ());
			r.setHobby (realInform.getHobby ());
			
			realInformDao.update (r);
		}
	}

	@Override
	public RealInform getRIById(long id)
	{
		
		return realInformDao.getById (id);
	}

	@Override
	public RealInform getRIByMemberId(long memberId)
	{
		
		return realInformDao.getByMemberId (memberId);
	}

	@Override
	public Page<RealInform> queryRealInform(Map<String, Object> params,int currentPageNum, int maxPageRowCount)
	{
		Page<RealInform> page = null;

		if (ObjectUtils.isNull(params))
		{
			params = new HashMap<String, Object>(2);
		}
		
		int count = realInformDao.count(params);

		if (count > 0)
		{
			page = new Page<RealInform>();
			page.setTotalRowCount(count);
			page.setCurrentPageNum(currentPageNum);
			page.setMaxPageRowCount(maxPageRowCount);
			page.setURL(params);

			params.put("startPageRowCount", page.getStartPageRowCount());
			params.put("maxPageRowCount", page.getMaxPageRowCount());

			List<Long> RealInformIds = realInformDao.query(params);

			if (ObjectUtils.notEmpty(RealInformIds))
			{
				List<RealInform> RealInforms = new ArrayList<RealInform>(RealInformIds.size());

				for (long RealInformId : RealInformIds)
				{
					RealInforms.add(getRIById (RealInformId));
				}

				page.setDatas(RealInforms);
			}
		}

		return page;
	}
}
